package cn.com.infohold.test.office;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import freemarker.template.Configuration;
import freemarker.template.Template;
 
public class FreemarkerDemo {
    public static void main(String[] args) throws Exception {
        //配置freemarker
        //得到Configuration对象
        Configuration config = new Configuration();
         
        //设置编码
        config.setDefaultEncoding("utf-8");
         
        //设置模版文件加载路径
        //1系统文件路径方式
        //config.setDirectoryForTemplateLoading(new File("C:/dev/mwebpay_eclipse/__testSomething/src/cn/com/xxx/test/office"));
        //2项目类路径方式
        config.setClassForTemplateLoading(FreemarkerDemo.class, "/cn/com/infohold/test/office");
         
        //得到模版对象
        Template temp = config.getTemplate("test.ftl");
         
        //准备数据
        Map root = new HashMap();
        List stuList = new ArrayList();
         
        Map student = null;
         
        student = new HashMap();
        student.put("stuno", "2010001");
        student.put("stuname", "xuh");
        student.put("stuage", "23");
        student.put("chinese", "89");
        student.put("math", "92");
        stuList.add(student);
         
        student = new HashMap();
        student.put("stuno", "2010002");
        student.put("stuname", "tom");
        student.put("stuage", "24");
        student.put("chinese", "72");
        student.put("math", "62");
        stuList.add(student);
         
        student = new HashMap();
        student.put("stuno", "2010003");
        student.put("stuname", "angela");
        student.put("stuage", 2100);
        student.put("chinese", "81");
        student.put("math", "94");
        stuList.add(student);
         
         
        //输出路径
        BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("out.doc")));
         
        //执行操作
        root.put("stuList", stuList);
        temp.process(root, bufw);
    }
}